package com.lu.security.cipher;

import org.jetbrains.annotations.NotNull;

import java.security.GeneralSecurityException;
import java.security.Key;

import javax.crypto.Cipher;

import com.lu.security.tools.HexTools;

/**
 * author: luqihua
 * date:2019-07-10
 * description: RSA
 **/
public class RSAEncrypt {
    private static final String TAG = "RSAEncrypt";

    private static final String RSA_MODEL = "RSA/ECB/PKCS1Padding";

    @NotNull
    private final Key mRSAKey;

    public RSAEncrypt(@NotNull Key rsaKey) {
        mRSAKey = rsaKey;
    }

    public byte[] encrypt(@NotNull byte[] data) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(RSA_MODEL);
        cipher.init(Cipher.ENCRYPT_MODE, mRSAKey);
        return cipher.doFinal(data);
    }

    public String encryptBase64(String data) throws GeneralSecurityException {
        return HexTools.encodeToBase64(encrypt(data.getBytes()));
    }
}
